package edu.upc.fib.lamsandroid.client.fragment.lesson;

import android.app.DownloadManager;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import com.google.gson.Gson;
import edu.upc.fib.lamsandroid.client.LamsAndroidClientApplication;
import edu.upc.fib.lamsandroid.client.R;
import edu.upc.fib.lamsandroid.client.Util;
import edu.upc.fib.lamsandroid.server.api.model.ToolContent;
import edu.upc.fib.lamsandroid.server.api.model.ToolContentDataSharedResources;
import edu.upc.fib.lamsandroid.server.api.model.ToolContentDataSharedResourcesItem;

public class SharedResourcesLessonFragment extends Fragment {

    private final ToolContent toolContent;

    public SharedResourcesLessonFragment(ToolContent toolContent) {
        this.toolContent = toolContent;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Context c = getActivity().getApplicationContext();

        LinearLayout layout = new LinearLayout(c);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT, 0);

        layout.setLayoutParams(params);

        final String urlPrefix = LamsAndroidClientApplication.getInstance().getFileRequestURLPrefix();

        layout.setOrientation(LinearLayout.VERTICAL);

        ToolContentDataSharedResources srContent
                = new Gson().fromJson(toolContent.getData().getJson(), ToolContentDataSharedResources.class);

        Util.addWebView(getActivity(), layout, srContent.getTitle());
        Util.addWebView(getActivity(), layout, srContent.getInstructions());

        for (ToolContentDataSharedResourcesItem item : srContent.getItems()) {

            Util.addWebView(getActivity(), layout, item.getTitle());

            for (String instructions : item.getInstructions()) {
                Util.addWebView(getActivity(), layout, instructions);
            }

            final String url = item.getUrl();
            if(url == null) {
                final String downloadTitle = item.getTitle();
                final String downloadDescription = item.getInstructions().size()>0?item.getInstructions().get(0):"";
                final String downloadURL = urlPrefix + item.getContentUUID();
                final String downloadFilename = item.getFileName();

                final Button button = new Button(layout.getContext());
                button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.download, 0, 0, 0);
                button.setText(item.getFileName());
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        downloadURL(downloadURL, downloadTitle, downloadDescription, downloadFilename);
                    }
                });

                layout.addView(button);
            } else {
                final Button button = new Button(layout.getContext());
                button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.url, 0, 0, 0);
                button.setText(url);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        final Uri uri = Uri.parse(url);
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        startActivity(intent);
                    }
                });
                layout.addView(button);
            }
        }

        ScrollView scrollView = new ScrollView(getActivity());
        scrollView.addView(layout);
        return scrollView;
    }

    private void downloadURL(final String url, final String title, final String description, final String filename) {
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.setDescription(description);
        request.setTitle(title);
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);

        DownloadManager manager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
        manager.enqueue(request);
    }
}
